package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.dao.CommunCommentMapper;
import com.example.blog.dao.CommunMemberMapper;
import com.example.blog.dto.AddCommentReqDto;
import com.example.blog.dto.QueryCommentResDto;
import com.example.blog.dto.QueryMemberResDto;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.entity.CommunComment;
import com.example.blog.entity.CommunMember;
import com.example.blog.response.CommonException;
import com.example.blog.service.CommunCommentService;
import com.example.blog.service.CommunMemberService;
import com.example.blog.util.ToolsUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Admin
 */
@Service
public class CommunCommentServiceImpl extends ServiceImpl<CommunCommentMapper, CommunComment> implements CommunCommentService {

    @Resource
    private CommunMemberService communMemberService;

    /**
     * 添加聊天记录
     * @param reqDto
     * @return
     */
    @Override
    public Boolean addComment(AddCommentReqDto reqDto){
        CommunMember communMember = communMemberService.getMemberStatus(reqDto.getNumberId(),reqDto.getRoomId());
        if(communMember.getIsMuted() == 1){
            throw new CommonException("该用户已被禁言，无法聊天");
        }
        CommunComment communComment = ToolsUtil.convertType(reqDto,CommunComment.class);
        communComment.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(communComment) > 0;
    }

    /**
     * 查询聊天记录
     * @param reqDto
     * @return
     */
    @Override
    public List<QueryCommentResDto> getComment(IdRequestDto reqDto){
        List<CommunComment> communCommentList = baseMapper.selectList(Wrappers.lambdaQuery(CommunComment.class)
                .eq(CommunComment::getRoomId,reqDto.getId())
                .orderByDesc(CommunComment::getCreateTime));
        return ToolsUtil.convertType(communCommentList, QueryCommentResDto.class);
    }

}
